﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraGrid.Views.Grid;
using SmallERP.Bll.WastePaperManage;
using SmallERP.MODEL.FoundationModel;

namespace SmallERP.WastePaperManage
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }
        WastePaperManageBLL _bll=new WastePaperManageBLL();

        List<PaperWasteRecord_info> srcList=new List<PaperWasteRecord_info>();
        private void FrmMain_Load(object sender, EventArgs e)
        {
            srcList = _bll.GetUnSureWasteRecord();
            gridControlWasteView.DataSource = srcList;

        }

        
        private void gridViewWasteView_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                PaperWasteRecord_info info = (PaperWasteRecord_info)gridViewWasteView.GetRow(e.RowHandle);
                if (e.Column.Name == "Status")
                {
                    //if(info.Status==(int)COM.Status.WasterPaperStatus.unSure)
                    //{
                    //    e.DisplayText = "未确认";
                    //}

                    switch (info.Status)
                    {
                        case (int)COM.Status.WasterPaperStatus.unSure:
                            e.DisplayText = "未确认";
                            break;
                        case (int)COM.Status.WasterPaperStatus.Sure:
                            e.DisplayText = "已确认";
                            break;
                        default:
                            break;
                            
                           
                    }

                }
            }
        }

        private void simpleButtonOK_Click(object sender, EventArgs e)
        {
            DateTime startTime = dateEditFrom.DateTime;
            DateTime endTime = dateEditTo.DateTime;
            if (string.IsNullOrEmpty(dateEditFrom.Text.Trim()))
            {
              startTime=DateTime.Now.AddMonths(-12);

            }
            if (string.IsNullOrEmpty(dateEditTo.Text.Trim()))
            {
                endTime = DateTime.Now;
            }
            srcList = _bll.GetUnSureWasteRecord(startTime, endTime);
            gridControlWasteView.DataSource = srcList;

        }

        private void textBoxPhotoBarCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == (int)Keys.Enter)
            {
                PaperWasteRecord_info info = new PaperWasteRecord_info();
                int wasteId = Convert.ToInt32(textBoxPhotoBarCode.Text.Trim());
                if (srcList.Count > 0)
                {
                    info = srcList.FirstOrDefault(p => p.Id == wasteId);
                    if (info == null)
                    {
                        if(_bll.MakeSureRecordSinger(wasteId)==0)
                        {
                            MessageBox.Show("未找到记录", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                        }
                        if (_bll.MakeSureRecordSinger(wasteId) == -1)
                        {
                            MessageBox.Show("修改失败，请稍后再试。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                        } else if (_bll.MakeSureRecordSinger(wasteId) == 2)
                        {
                            MessageBox.Show("废片已确认过。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            
                        }
                        else
                        {
                            srcList = _bll.GetUnSureWasteRecord();
                            gridControlWasteView.DataSource = srcList;
                          //  MessageBox.Show("确认成功。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            labelSuccess.Text= wasteId+"确认成功！";
                            textBoxPhotoBarCode.Text = "";

                        }
                       
                    }
                    else
                    {

                        if (info.Status == (int)COM.Status.WasterPaperStatus.Sure)
                        {
                            MessageBox.Show("废片已确认过。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                        }
                        else
                        {
                            _bll.MakeSureRecordSinger(wasteId);
                            info.Status = (int)COM.Status.WasterPaperStatus.Sure;
                            //MessageBox.Show("确认成功。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            labelSuccess.Text = wasteId + "确认成功！";
                            textBoxPhotoBarCode.Text = "";
                           
                        }
                        
                    }

                }
                else
                {
                    srcList = _bll.GetUnSureWasteRecord();
                    gridControlWasteView.DataSource = srcList;
                   // viewList = _bll.MakeSureRecord(wasteId);
                    if (srcList.Count == 0)
                    {
                        MessageBox.Show("未找到记录", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }

                gridControlWasteView.DataSource = srcList;
                gridControlWasteView.RefreshDataSource();


            }
        }

        private void gridViewWasteView_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            GridView view = sender as GridView;
            if(e.RowHandle>=0)
            {
                PaperWasteRecord_info info = (PaperWasteRecord_info)view.GetRow(e.RowHandle);
                if(info.Status==(int)SmallERP.COM.Status.WasterPaperStatus.Sure)
                {
                    e.Appearance.BackColor = Color.LightGreen;
                }

            }
        }
    }
}
